// pages/cart/cart.js

Page({
  /**
   * 页面的初始数据
   */
  data: {
    cartlist: [],
    showcart: true,
    total: 0
  },

  // 去结算
  toiforder() {
    wx.navigateTo({
      url: '/pages/iforder/iforder',
    })
  },

  // 点击复选框
  ifstatus(e) {
    let itemIndex = e.currentTarget.dataset.ifindex
    let newcart = wx.getStorageSync('cart') || []
    newcart[itemIndex].sellStart = !newcart[itemIndex].sellStart
    this.setData({
      cartlist: newcart
    })
    wx.setStorageSync('cart', newcart)
    this.comprice()
  },

  // 合计
  comprice() {
    let newcart = wx.getStorageSync('cart') || []
    let ztotal = 0
    newcart.map(item => {
      if (item.sellStart) {
        ztotal += (item.minPrice * item.minBuyNumber)
      }
    })
    this.setData({
      total: ztotal
    })
  },

  // 数量减少
  rednum(e) {
    let itemIndex = e.currentTarget.dataset.redindex
    let cart = wx.getStorageSync('cart') || []
    if (cart[itemIndex].minBuyNumber <= 1) {
      cart.splice(itemIndex, 1)
    } else {
      cart[itemIndex].minBuyNumber--
    }
    this.setData({
      cartlist: cart
    })
    wx.setStorageSync('cart', cart)
    this.comcartnum()
    this.comprice()
    this.getcart()
  },

  // 数量增加
  addnum(e) {
    let itemIndex = e.currentTarget.dataset.addindex
    let cart = wx.getStorageSync('cart') || []
    cart[itemIndex].minBuyNumber++
    this.setData({
      cartlist: cart
    })
    wx.setStorageSync('cart', cart)
    this.comcartnum()
    this.comprice()
  },

  // 跳转到home页
  tohome() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },

  // 获取购物车数据
  getcart() {
    // 获取购物车数据
    let cart = wx.getStorageSync('cart') || []
    if (cart.length === 0) {
      this.setData({
        showcart: true
      })
    } else {
      this.setData({
        showcart: false
      })
    }
    this.setData({
      cartlist: cart
    })
  },

  // 计算购物车总数量
  comcartnum() {
    let cart = wx.getStorageSync('cart') || []
    let cartnum = 0
    cart.map(item => {
      cartnum += item.minBuyNumber
    })
    wx.setTabBarBadge({
      index: 3,
      text: String(cartnum),
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 判断是否登录
    let token = wx.getStorageSync('token') || ''
    if (token === '') {
      wx.switchTab({
        url: '/pages/my/my',
      })
    }

    this.getcart()
    this.comcartnum()
    this.comprice()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 判断是否登录
    let token = wx.getStorageSync('token') || ''
    if (token === '') {
      wx.switchTab({
        url: '/pages/my/my',
      })
    }
    
    this.getcart()
    this.comprice()
    this.comcartnum()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})